#include "LeptonicZReconstructor.h"


LeptonicZReconstructor::LeptonicZReconstructor()
{

}

LeptonicZReconstructor::LeptonicZReconstructor ( EvtData* e)
{
LeptonicZReconstructor::evtdata=e;
ZContainer=new std::vector<ZCandidate>;

  
}

LeptonicZReconstructor::~LeptonicZReconstructor()
{
ZContainer->clear();
delete ZContainer;
}

std::vector<ZCandidate>* LeptonicZReconstructor::ZCandidates()
{
return LeptonicZReconstructor::ZContainer;
}


ZCandidate LeptonicZReconstructor::getZ()
{
return LeptonicZReconstructor::Z;
}

bool LeptonicZReconstructor::minimize(ZCandidate z1,ZCandidate z2){
  
  return( fabs((z1.m_electron.P4()+z1.m_positron.P4()).M()-MZ )<fabs((z2.m_electron.P4()+z2.m_positron.P4()).M()-MZ ) );
}

void LeptonicZReconstructor::Run(){
  ZCandidate zc;
  for(unsigned int i=0;i<evtdata->getElectrons()->size();i++){//electron-positron loop
     if(evtdata->getElectrons()->at(i).Charge()<0){ 
     zc.m_electron=evtdata->getElectrons()->at(i);
     zc.i_electron=i;
     }
     else if(evtdata->getElectrons()->at(i).Charge()>0){
     zc.m_positron=evtdata->getElectrons()->at(i);
     zc.i_positron=i;
     }
     else std::cout<<"WARNING::LeptonicZReconstructor electron charge seems to be zero ...."<<std::endl;
     zc.deltaM=fabs( (zc.m_electron.P4()+zc.m_positron.P4()).M()-MZ );
     ZContainer->push_back(zc);
  }
  //std::cout<<"Retrieved "<<ZContainer->size()<<" Z candidates"<<std::endl;
  std::sort(ZContainer->begin(),ZContainer->end(),minimize);
  double MZcand=(ZContainer->at(0).m_electron.P4()+ZContainer->at(0).m_positron.P4() ).M();
  LeptonicZReconstructor::Z=ZContainer->at(0);
}